package leetcodev1.链表;

import com.sun.xml.internal.bind.v2.model.core.ID;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class LeetCode146 {

}

class LRUCache {
    private int capacity;
    private Map<Integer, ListNode> dict;
    private ListNode head, tail;
    private ListNode noFoundNode = new ListNode(-1);

    public LRUCache(int capacity) {
        this.capacity = capacity;
        dict = new HashMap<>(capacity);
    }

    public int get(int key) {
        if(!dict.containsKey(key)){
            return -1;
        }
        ListNode listNode = dict.get(key);
        if (listNode.pre != null) {
            listNode.pre.next = listNode.next;
        }
        listNode.pre = tail;
        listNode.next = null;
        return listNode.val;
    }

    public void put(int key, int value) {
        if (dict.containsKey(key)) {
            ListNode listNode = dict.get(key);
            listNode.val = value;
            if (listNode.pre != null) {
                listNode.pre.next = listNode.next;
            }
            listNode.pre = tail;
            listNode.next = null;
            tail = listNode;
            return;
        }

        if (capacity < 0) {
            dict.remove(head.key);
            head = head.next;
        }

        ListNode listNode = new ListNode(key, value);
        dict.put(key, listNode);
        capacity--;
        if (head == null) {
            head = listNode;
        } else {
            listNode.pre = tail;
        }
        tail = listNode;
    }
}

//牛皮
//使用map的删除策略
class LRUCache1 extends LinkedHashMap<Integer, Integer> {
    private int capacity;

    public LRUCache1(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}

//hashmap+双向链表
//更新永远=删除当前节点，插入到头部 | 尾部
class LRUCache2 {
    class DLinkedNode {
        int key;
        int value;
        DLinkedNode prev;
        DLinkedNode next;
        public DLinkedNode() {}
        public DLinkedNode(int _key, int _value) {key = _key; value = _value;}
    }

    private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>();
    private int size;
    private int capacity;
    private DLinkedNode head, tail;

    public LRUCache2(int capacity) {
        this.size = 0;
        this.capacity = capacity;
        // 使用伪头部和伪尾部节点
        head = new DLinkedNode();//不用判空
        tail = new DLinkedNode();//不用判空
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        DLinkedNode node = cache.get(key);
        if (node == null) {
            return -1;
        }
        // 如果 key 存在，先通过哈希表定位，再移到头部
        moveToHead(node);
        return node.value;
    }

    public void put(int key, int value) {
        DLinkedNode node = cache.get(key);
        if (node == null) {
            // 如果 key 不存在，创建一个新的节点
            DLinkedNode newNode = new DLinkedNode(key, value);
            // 添加进哈希表
            cache.put(key, newNode);
            // 添加至双向链表的头部
            addToHead(newNode);
            ++size;
            if (size > capacity) {
                // 如果超出容量，删除双向链表的尾部节点
                DLinkedNode tail = removeTail();
                // 删除哈希表中对应的项
                cache.remove(tail.key);
                --size;
            }
        }
        else {
            // 如果 key 存在，先通过哈希表定位，再修改 value，并移到头部
            node.value = value;
            moveToHead(node);
        }
    }

    //双向指针指向头部
    private void addToHead(DLinkedNode node) {
        node.prev = head;//永远指向虚拟节点
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
    }

    //双向链表删除节点
    private void removeNode(DLinkedNode node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    //移动到头部
    private void moveToHead(DLinkedNode node) {
        removeNode(node);
        addToHead(node);
    }

    //删除尾节点
    private DLinkedNode removeTail() {
        DLinkedNode res = tail.prev;
        removeNode(res);
        return res;
    }
}
